<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="5" :xs="24">
        <my-dept-tree v-model="queryParams.deptId" @change="handleDeptChange" ref="deptTree"></my-dept-tree>
      </el-col>
      <el-col :span="19" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
          <el-form-item label="日期" prop="date">
            <date-range-picker startPlaceholder="开始日期" endPlaceholder="结束日期" v-model="dateRange"></date-range-picker>
          </el-form-item>
          <el-form-item label="姓名" prop="userId">
            <dept-user-select ref="deptUserSelect"  :deptId="queryParams.deptId" :default-select="true" v-model="queryParams.userId" placeholder="请选择排查人" @change="handleQuery"></dept-user-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini"  @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['cases:statisticsDaily:export']" type="warning" plain  icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
          </el-col>
        </el-row>
        <el-table v-loading="loading" :row-style="rowStyle" :highlight-current-row="true" :data="tableData">
          <el-table-column type="index" :index="(index)=>tableIndex(queryParams.pageSize,queryParams.pageNum,index)" label="序号" align="center" />
          <el-table-column label="日期" align="center" prop="statisticsDate" width="150" />
          <el-table-column label="工区" align="center" prop="deptName" width="150" />
          <el-table-column label="姓名" align="center" prop="username" width="100" />
          <el-table-column label="工作量明细" align="center" prop="statisticsDetail" min-width="250" show-overflow-tooltip/>
          <el-table-column label="额定系数" align="center" prop="ratedCoefficient" width="100" />
          <el-table-column label="考核人" align="center" prop="assessorName" width="100" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetail(scope.row)">查看明细</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
      </el-col>
    </el-row>
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="900px" append-to-body :before-close="handleClose">
      <el-table v-loading="detailLoading" :data="detailTableData">
        <el-table-column type="index" :index="(index)=>index + 1" label="序号" align="center" />
        <el-table-column label="养护作业项目" align="center" prop="quantitativeTypeName" width="200" />
        <el-table-column label="定额定量" align="center" prop="quantitativeTypeValue" width="200" />
        <el-table-column label="验收后实际工作量" align="center" prop="qualifiedNum" width="200" />
        <el-table-column label="人工单位成本" align="center" prop="quantitativeLaborCost" width="200" />
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="updateDialogTitle" :close-on-click-modal="false" :visible.sync="updateDialogOpen" width="400px" append-to-body :before-close="handleCloseUpdateDialog">
      <el-form :model="form" ref="form" size="small" :rules="rules">
        <el-form-item label="定额标准" prop="quotaStandard">
          <el-input type="number" v-model="form.quotaStandard" placeholder="请输入定额标准" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">保 存</el-button>
        <el-button @click="cancelUpdate">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listStatisticsDaily,
  getStatisticsDailyDetail,
  updateStatisticsDaily,
} from "@/api/cases/statisticsDaily";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import CommonCheckTypeSelect from '../../../components/Select/CommonCheckTypeSelect.vue';
import DictSelect from '../../../components/Select/DictSelect.vue';
import DeptUserSelect from '../../../components/Select/DeptUserSelect.vue';
import DeptTree from '../../../components/Tree/DeptTree.vue';
import DateRangePicker from '../../../components/DatePicker/DateRangePicker.vue';
import { defaultDateDangeInMonth } from "@/utils/index.js"
import { mapGetters } from 'vuex';
import MyDeptTree from '../../../components/Tree/MyDeptTree.vue';

export default {
  name: "statisticsDaily",
  dicts: ["jl_cases_damage_level", "jl_cases_repair_method", "jl_data_status"],
  components: { DictSelect, CommonCheckTypeSelect, DictSelect, DeptUserSelect, DeptTree, DateRangePicker, MyDeptTree },
  data() {
    return {
      // 禁用表单
      isDisabled: false,
      // 遮罩层
      loading: true,
      //明细遮罩层
      detailLoading: true,
      // 选中数组
      ids: [],
      //选中行
      selection: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      tableData: [],
      // 明细表格数据
      detailTableData: [],
      // 弹出层标题
      title: "",
      // 更新弹出层标题
      updateDialogTitle: "",
      // 是否显示弹出层
      open: false,
      // 是否显示更新弹出层
      updateDialogOpen: false,
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: void 0,
        startDate: void 0,
        endDate: void 0,
        deptId: void 0
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.dateRange = defaultDateDangeInMonth();
  },
  computed: {
    ...mapGetters(["userId"]),
    // 表单校验
    rules() {
      return {
        quotaStandard: [
          { required: true, message: "定额标准不能为空", trigger: "blur" },
        ],
      }
    },
  },
  watch: {
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.queryParams.startDate = this.dateRange[0];
      this.queryParams.endDate = this.dateRange[1];
      this.loading = true;
      listStatisticsDaily(this.queryParams).then((response) => {
        this.tableData = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: void 0,
        quotaStandard: void 0,
      };
      this.resetForm("form");
    },
    /**
     * 部门改变
     */
    handleDeptChange(){
      this.$refs.deptUserSelect.clear();
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.dateRange = defaultDateDangeInMonth();
      this.handleQuery();
    },
    /** 查看 */
    handleDetail(row) {
      const userId = row.userId;
      const statisticsDate = row.statisticsDate;
      this.detailLoading = true;
      this.open = true;
      this.title = "定额工作完成情况评价明细--" + row.username;
      getStatisticsDailyDetail({
        userId,
        statisticsDate
      }).then((response) => {
        this.detailTableData = response;
        this.detailLoading = false;
      });
    },
    /**
     * 处理设置定额标准
     */
    handleUpdateDebz(row) {
      this.updateDialogOpen = true;
      this.reset();
      this.form = Object.assign({}, row);
      this.updateDialogTitle = "设置定额标准"
    },
    /**
     * 提交表单
     */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          updateStatisticsDaily(this.form).then(({ code, data, msg }) => {
            if (code == 200) {
              this.$modal.msgSuccess("设置成功");
              this.handleCloseUpdateDialog();
              this.getList();
            } else {
              this.$modal.msgError(msg);
            }
          });
        }
      });
    },
    /**
     * 审核
     */
    handleUpdateSh(row) {
      if(!row.quotaStandard){
         this.$modal.msg("定额标准未设置，暂不能审核");
        return;
      }
      if(row.auditUserId){
         this.$modal.msg("审核后不可再次修改额定标准");
        return;
      }
      let data = Object.assign({}, row, { auditUserId: this.userId });
      this.$modal.confirm("是否确认完成审核？审核后不可再次修改额定标准").then(() => {
        updateStatisticsDaily(data).then(({ code, data, msg }) => {
          if (code == 200) {
            this.$modal.msgSuccess("审核完成");
            this.getList();
          } else {
            this.$modal.msgError(msg);
          }
        });
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/cases/statisticsDaily/exportTest', {
        ...this.queryParams
      }, `个人评价_${new Date().getTime()}.xlsx`)
    },
    /**
     * 行样式
     */
    rowStyle() {
      return {
        "cursor": "pointer"
      }
    },
    /**
     * 处理关闭
     */
    handleClose() {
      this.cancel();
    },
    /**
     * 隐藏
     */
    cancel() {
      this.open = false;
    },
    /**
     * 处理关闭
     */
    handleCloseUpdateDialog() {
      this.cancelUpdate();
    },
    /**
     * 取消更新
     */
    cancelUpdate() {
      this.updateDialogOpen = false;
    },
  },
};
</script>
<style lang="scss" scoped>
.form-title {
  padding: 2px 0;
  border-bottom: 1px solid #eee;
  margin-bottom: 10px;
}
.input-sperator {
  margin-right: 10px;
  margin-left: 10px;
  padding-bottom: 18px;
  line-height: 29px;
}
</style>
